﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using MyWcfLib;
using WeatherService.Helpers;
using WeatherService.Storage;
using WeatherService.Types;

namespace WeatherService.WCF
{
    internal class MyWcfServer : IMyWcfServiceControl
    {

        //[PrincipalPermission(SecurityAction.Demand, Role = @"gb\domain users")]
        //[PrincipalPermission(SecurityAction.Demand, Role = @"gb\domain admins")]
        public MyWcfServiceResponse ExecuteCommand(MyWcfServiceCommand command)
        {
            var response = new MyWcfServiceResponse
                                                {
                                                    Response = command.Command + " Server Responded: " + DateTime.Now.ToString("ddd, d MMM yyyy HH:mm")
                                                };
            try
            {





                var dataList = new List<Weather>();
                var data = new Weather
                               {
                                   API = command.API,
                                   areaID = command.areaID,
                                   deviceInst = command.deviceInst,
                                   scTemp = command.scTemp,
                                   shTemp = command.shTemp,
                                   slTemp = command.slTemp,
                                   swspd = command.swspd

                               };
                dataList.Add(data);
                IsolatedStorageCacheManager<Weather>.SerializeDataListToXML(dataList, "weatherData");
                response.API = dataList[0].API;
                response.areaID = dataList[0].areaID;
                response.deviceInst = dataList[0].deviceInst;
                response.scTemp = dataList[0].scTemp;
                response.shTemp = dataList[0].shTemp;
                response.slTemp = dataList[0].slTemp;
                response.swspd = dataList[0].swspd;
            }
            catch (Exception ex)
            {
                var msg = "Error occurred trying to store updated weather parameters: " + ex.Message;
                Logger.ErrorLog(msg);
                response.Response=msg;
            }
            return response;
        }
        public MyWcfServiceResponse ExecuteRead(MyWcfServiceCommand command)
        {
            var response = new MyWcfServiceResponse();
            try
            {

                var dataList = IsolatedStorageCacheManager<Weather>.DeserializeDataFromXML("weatherData");
                if (dataList != null)
                {
                    if (dataList.Count > 0)
                    {
                        response.deviceInst = dataList[0].deviceInst;
                        response.API = dataList[0].API;
                        response.areaID= dataList[0].areaID;
                        response.slTemp= dataList[0].slTemp;
                        response.shTemp = dataList[0].shTemp;
                        response.scTemp = dataList[0].scTemp;
                        response.swspd = dataList[0].swspd;
                        response.Response = "Success";
                    }
                    else
                    {
                        response.Response="No records present";
                    }
                }
                else
                {
                    response.Response = "No local store available";
                }
            }
            catch (Exception ex)
            {
                var msg = "Error occurred trying to store updated weather parameters: " + ex.Message;
                Logger.ErrorLog(msg);
                response.Response = msg;
            }
            return response;
        }
    }
}
